
package org.itboys.mobile.controller;

import com.google.common.collect.Maps;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.OrderShopGift;
import org.itboys.mobile.entity.mongo.ShopGift;
import org.itboys.mobile.entity.mongo.SundayAddress;
import org.itboys.mobile.service.mongo.mobile.OrderShopGiftService;
import org.itboys.mobile.service.mongo.mobile.ShopGiftService;
import org.itboys.mobile.service.mongo.mobile.SundayMobileAddressService;
import org.itboys.mobile.util.MobileSignUtils;
import org.itboys.mobile.dto.SundayMemberDto;
import org.itboys.mobile.dto.order.KitchenOrderDto;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.service.mongo.mobile.SundayMobileMemberService;

import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * 作者：jiangxiong
 * 日期：2017年2月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_用户_controller。
 */

@Controller
@RequestMapping("/sunday/mobile/member")
public class SundayMobileMemberController extends BaseController{
    @Autowired
    private SundayMobileMemberService memberService;
    @Autowired
    private ResourceHolder resourceHolder;
    @Autowired
    private ShopGiftService shopGiftService;
    @Autowired
    private SundayMobileAddressService addressService;
    @Autowired
    private OrderShopGiftService orderShopGiftService;



/**
     * 【用户版】用户注册。手机号和密码
     * @param request
     * @param response
     * @param mobile
     * @param password
     * @param code
     */

    @RequestMapping(value="/register", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】用户注册。手机号和密码。-1直接提示消息")
    public void register(HttpServletRequest request,
                     HttpServletResponse response,
                     @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                     @ApiParam(required=true, name="password", value="密码。明文")@RequestParam(value = "password", required = true)String password,
                     @ApiParam(required=true, name="code", value="验证码")@RequestParam(value = "code", required = true)String code

                         ){
        try{

            SundayMemberDto member= memberService.register( mobile,password,code);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgResultDO(member,CommonConstants.SUCCESS));
        }catch (Exception e){

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.info("/sunday/mobile/member/register--------"+e);
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }
    }

/**
     * 【用户版和管理版】用户登陆。手机号和密码。-1直接提示消息
     * @param request
     * @param response
     * @param mobile
     * @param password
     */

    @RequestMapping(value="/login", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】用户登陆。手机号和密码。-1直接提示消息")
    public void login(HttpServletRequest request,
                         HttpServletResponse response,
                         @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                         @ApiParam(required=true, name="password", value="密码。明文")@RequestParam(value = "password", required = true)String password,
                         @ApiParam(required=true, name="loginType", value="登陆类型。1用户版，2管理版")@RequestParam(value = "loginType", required = true)Integer loginType

    ){
        try{


            //Long memberId = AccountIdHoldUtils.getMemberId();
            SundayMemberDto member= memberService.login( mobile,password,loginType);
            ResponseMessage.success(member);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/login----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

/**
     * 用户版】第三方登陆。第一步。（共三步）。-1直接提示消息；-2，执行第第二步。
     * @param request
     * @param response
     * @param thirdType
     * @param unionId
     */
/*
    @RequestMapping(value="/loginWithThirdOne", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】第三方登陆。第一步。（共三步）。-1直接提示消息；-2，执行第二步。")
    public void loginWithThirdOne(HttpServletRequest request,
                      HttpServletResponse response,
                      @ApiParam(required=true, name="thirdType", value="第三方登陆类型1,微信，2qq,3，微博")@RequestParam(value = "thirdType", required = true)Integer thirdType,
                      @ApiParam(required=true, name="unionId", value="第三方用户标识")@RequestParam(value = "unionId", required = true)String unionId){

        try{

            SundayMemberDto member= memberService.loginWithThirdOne(thirdType, unionId);
            if(member==null){
                ResponseMessage.error(-2,"需要验证手机号");
            }else{
                ResponseMessage.success(member);
            }

        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/loginWithThirdOne----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
*/

/**
     * 【用户版】第三方登陆。第二步。（共三步）。-1直接提示消息；-2，执行第三步。
     * @param request
     * @param response
     * @param thirdType
     * @param nickName
     * @param image
     * @param unionId
     * @param mobile
     * @param code
     *//*

    @RequestMapping(value="/loginWithThirdTwo", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】第三方登陆。第二步。（共三步）。-1直接提示消息；-2，执行第三步。")
    public void loginWithThirdTwo(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @ApiParam(required=true, name="thirdType", value="第三方登陆类型1,微信，2qq,3，微博")@RequestParam(value = "thirdType", required = true)Integer thirdType,
                                  @ApiParam(required=true, name="nickName", value="第三方昵称")@RequestParam(value = "nickName", required = true)String nickName,
                                  @ApiParam(required=true, name="image", value="第三方头像")@RequestParam(value = "image", required = true)String image,
                                  @ApiParam(required=true, name="unionId", value="第三方用户标识")@RequestParam(value = "unionId", required = true)String unionId,
                                  @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                                  @ApiParam(required=true, name="code", value="验证码")@RequestParam(value = "code", required = true)String code){

        try{

            SundayMemberDto member= memberService.loginWithThirdTwo( thirdType, nickName, image,  unionId,  mobile, code);
            if(member==null){
                ResponseMessage.error(-2,"需要选择您所在的小区");
            }else{
                ResponseMessage.success(member);
            }

        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/loginWithThirdTwo----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }


     * 【用户版】第三方登陆。第二步。（共三步）。-1直接提示消息；-2，执行第三步。
     * @param request
     * @param response
     * @param thirdType
     * @param nickName
     * @param image
     * @param unionId
     * @param mobile
     * @param code
     * @param password
     * @param villageId
     */
/*
    @RequestMapping(value="/loginWithThirdThree", method= RequestMethod.POST)
    @ApiOperation(value="【用户版】第三方登陆。第三步。（共三步）。-1直接提示消息；")
    public void loginWithThirdThree(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @ApiParam(required=true, name="thirdType", value="第三方登陆类型1,微信，2qq,3，微博")@RequestParam(value = "thirdType", required = true)Integer thirdType,
                                  @ApiParam(required=true, name="nickName", value="第三方昵称")@RequestParam(value = "nickName", required = true)String nickName,
                                  @ApiParam(required=true, name="image", value="第三方头像")@RequestParam(value = "image", required = true)String image,
                                  @ApiParam(required=true, name="unionId", value="第三方用户标识")@RequestParam(value = "unionId", required = true)String unionId,
                                  @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                                  @ApiParam(required=true, name="code", value="验证码")@RequestParam(value = "code", required = true)String code,
                                  @ApiParam(required=true, name="password", value="密码（明文）")@RequestParam(value = "code", required = true)String password,
                                  @ApiParam(required=true, name="villageId", value="小区ID")@RequestParam(value = "villageId", required = true)Long villageId){

        try{

            SundayMemberDto member= memberService.loginWithThirdThree( thirdType, nickName, image,  unionId,  mobile, code, password,villageId);
            ResponseMessage.success(member);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/loginWithThirdThree----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }
*/

/**
     * 【用户版和管理版】退出登录。-1直接提示消息；
     * @param request
     * @param response

     * @param nonce
     * @param sign
     */

    @RequestMapping(value="/logout", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】退出登录。-1直接提示消息；")
    public void logout(HttpServletRequest request,
                                  HttpServletResponse response,
                                   @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                                   @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
//            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
//            param.put("nonce", nonce);
//            param.put("sign", sign);
//
//            boolean signSer = MobileSignUtils.signCheck(param, sign);
//            if(!signSer){
//                logger.warn("签名校验失败");
//                ResponseMessage.error(-1,"签名校验失败");
//                return;
//            }

            Long memberId = AccountIdHoldUtils.getMemberId();
            memberService.logout(memberId);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/logout----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

/**
     * 【用户版和管理版】修改个人信息。-1直接提示消息；
     * @param request
     * @param response
     * @param nickName
     * @param mobileName
     * @param address
     * @param email
     * @param image
     * @param idCard
     * @param desc
     * @param nonce
     * @param sign
     */

    @RequestMapping(value="/update", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】修改个人信息。-1直接提示消息；会返回新的token，记得更新移动端的token")
    public void update(HttpServletRequest request,
                       HttpServletResponse response,

                       @ApiParam(required=false, name="nickName", value="昵称")@RequestParam(value = "nickName", required = false)String nickName,
                       @ApiParam(required=false, name="mobileName", value="联系人")@RequestParam(value = "mobileName", required = false)String mobileName,
                       @ApiParam(required=false, name="address", value="地址")@RequestParam(value = "address", required = false)String address,
                       @ApiParam(required=false, name="email", value="电子邮箱")@RequestParam(value = "email", required = false)String email,
                       @ApiParam(required=false, name="image", value="头像")@RequestParam(value = "image", required = false)String image,
                       @ApiParam(required=false, name="idCard", value="身份证号码")@RequestParam(value = "idCard", required = false)String idCard,
                       @ApiParam(required=false, name="desc", value="个人简介")@RequestParam(value = "desc", required = false)String desc,
                       @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                       @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("nickName", nickName);

            param.put("mobileName", mobileName);
            param.put("address", address);
            param.put("email", email);
            param.put("image", image);
            param.put("idCard", idCard);
            param.put("desc", desc);
//            param.put("nonce", nonce);
//            param.put("sign", sign);
//
//            boolean signSer = MobileSignUtils.signCheck(param, sign);
//            if(!signSer){
//                logger.warn("签名校验失败");
//                ResponseMessage.error(-1,"签名校验失败");
//                return;
//            }

            Long memberId = AccountIdHoldUtils.getMemberId();
           SundayMemberDto result =memberService.update( memberId, nickName,mobileName, address, email, image, idCard, desc);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/update----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }


/**
     * 【用户版和管理版】修改密码。使用旧密码修改
     * @param request
     * @param response
     * @param oldPassword
     * @param newPassword

     */

    @RequestMapping(value="/updatePassword", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】修改密码。使用旧密码修改。-1直接提示消息；")
    public void updatePassword(HttpServletRequest request,
                       HttpServletResponse response,

                       @ApiParam(required = true, name = "oldPassword", value = "旧密码") @RequestParam(value = "oldPassword", required = true) String oldPassword,
                       @ApiParam(required = true, name = "newPassword", value = "新密码") @RequestParam(value = "newPassword", required = true) String newPassword,
                       @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                       @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("oldPassword", oldPassword);
            param.put("newPassword", newPassword);
//            param.put("nonce", nonce);
//            param.put("sign", sign);
           // boolean signSer = MobileSignUtils.signCheck(param, sign);


            Long memberId = AccountIdHoldUtils.getMemberId();
            memberService.updatePassword(memberId,oldPassword, newPassword);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/updatePassword----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }


/**
     * 用户版和管理版】修改密码。使用验证码修改
     * @param request
     * @param response
     * @param mobile
     * @param newPassword
     * @param code
     */

    @RequestMapping(value="/updatePasswordWithCode", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】修改密码。使用验证码修改。-1直接提示消息；")
    public void updatePasswordWithCode(HttpServletRequest request,
                               HttpServletResponse response,
                               @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                               @ApiParam(required = true, name = "newPassword", value = "新密码") @RequestParam(value = "newPassword", required = true) String newPassword,
                               @ApiParam(required = true, name = "code", value = "验证码") @RequestParam(value = "code", required = true) String code,
                               @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                               @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("mobile", mobile);
            param.put("newPassword", newPassword);
            param.put("code", code);
//            param.put("nonce", nonce);
//            param.put("sign", sign);
//            boolean signSer = MobileSignUtils.signCheck(param, sign);

/* if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }*/

            Long memberId = AccountIdHoldUtils.getMemberId();
           // memberService.updatePassword(memberId,oldPassword, newPassword);
            memberService.updatePasswordWithCode(memberId,mobile,newPassword,code);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/updatePasswordWithCode----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }

/**
     * 用户版和管理版】忘记密码
     * @param request
     * @param response
     * @param mobile
     * @param newPassword
     * @param code
     */

    @RequestMapping(value="/forgetPassword", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】忘记密码。。-1直接提示消息；")
    public void forgetPassword(HttpServletRequest request,
                                       HttpServletResponse response,
                                        @ApiParam(required=true, name="mobile", value="手机号")@RequestParam(value = "mobile", required = true)String mobile,
                                       @ApiParam(required = true, name = "newPassword", value = "新密码") @RequestParam(value = "newPassword", required = true) String newPassword,
                                       @ApiParam(required = true, name = "code", value = "验证码") @RequestParam(value = "code", required = true) String code,
                               @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                               @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){

        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("mobile", mobile);
            param.put("newPassword", newPassword);
            param.put("code", code);
//            param.put("nonce", nonce);
//            param.put("sign", sign);
//            boolean signSer = MobileSignUtils.signCheck(param, sign);

//  if(!signSer){
//                logger.warn("签名校验失败");
//                ResponseMessage.error(-1,"签名校验失败");
//                return;
//            }

            memberService.forgetPassword(mobile,newPassword,code);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/forgetPassword----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }


/**
     * 获取门店子账号
     * @param request
     * @param response
     *
     */

    @RequestMapping(value="/findOne", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】获取用户详情，移动端同时更新用户token；")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response ,
            @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            SundayMemberDto result=memberService.findOne(memberId);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/member/findOne----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }

/**
     * 个人分享页面
     * @param request
     * @param model
     * @return
     */

    @RequestMapping(value="/share",method={ RequestMethod.POST,RequestMethod.GET} )
    public  String share(HttpServletRequest request,
                         Model model,
                         @RequestParam(value="id",required=true) long id){
        SundayMember member = memberService.getById(id);

        model.addAttribute("image",member.getImage());
        model.addAttribute("nickName",member.getNickName());
        return "/sunday/member/share";
    }

    /**
     * @author 朱坤涛
     *
     */
    @ApiOperation(value = "开店礼包列表")
    @RequestMapping(value = "/AgShopGIft",method = RequestMethod.POST)
    public void AsShopGift(HttpServletRequest request,HttpServletResponse response){
        try {
            String imRoot = resourceHolder.getStringValue("imRoot");
            Map<String,Object> map = new HashMap<>();
            List<ShopGift> list = shopGiftService.list(map);
            for (ShopGift gift:list){
                gift.setImage(imRoot+gift.getImage());
            }
            AjaxUtils.renderJson(response,ResultPageDOHelper.getMsgResultDO(list,"success!"));
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJson(response,ResultPageDOHelper.getErrorResultDO(-1,"error!"));
        }
    }

    @ApiOperation(value = "开店创建订单")
    @RequestMapping(value = "/AhCreateOrder",method = RequestMethod.POST)
    public void AhCreateOrder(
            @ApiParam(required = true,name = "addressId",value = "地址Id")@RequestParam(value = "addressId",required = true)Long addressId,
            @ApiParam(required = true,name = "refId",value = "礼包Id")@RequestParam(value = "refId",required = true)Long refId,
            HttpServletRequest request,HttpServletResponse response
    ){
        try {
            SundayAddress address = addressService.getById(addressId);
            SundayMember member = memberService.getById(address.getMemberId());
            if (member!=null){
                OrderShopGift order = orderShopGiftService.createOrder(refId,address);
                AjaxUtils.renderJson(response,ResultPageDOHelper.getMsgResultDO(order,"success!"));
            }else {
                AjaxUtils.renderJson(response,ResultPageDOHelper.getErrorResultDO(-2,"会员不存在！"));
            }
    }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJson(response,ResultPageDOHelper.getErrorResultDO(-1,"error!"));
        }
    }

    @ApiOperation(value = "输入邀请码")
    @RequestMapping(value = "/AjBindShop", method = RequestMethod.POST)
    public void AjBindShop(
            @ApiParam(required = true,name = "memberId",value = "用户Id")@RequestParam(value = "memberId",required = true)Long memberId,
            @ApiParam(required = true,name = "initCode",value = "店主邀请码")@RequestParam(value = "code",required = true)String initCode,
            HttpServletRequest request,HttpServletResponse response
    ){
        try {
            String code = initCode.toUpperCase();
            SundayMember member = memberService.getById(memberId);
            SundayMember shop = memberService.getByField("initCode",code);
            List<SundayMember> memberList = memberService.findByField("recId",memberId);
            if (shop!=null&&shop.getMemberLevel()==2){
                if (code.equals(member.getInitCode())){
                    AjaxUtils.renderJson(response,ResultPageDOHelper.getErrorResultDO(-4,"不能填写自己的邀请码！"));
                    return;
                }
                if (memberList!=null&&memberList.size()>0){
                    AjaxUtils.renderJson(response,ResultPageDOHelper.getErrorResultDO(-3,"已经发展粉丝不能填写邀请码！"));
                    return;
                } else {
                    if (member.getRecId()==null||member.getRecId()==0){
                        member.setRecId(shop.getId());
                        member.setPath(shop.getPath()+member.getId()+"/");
                        memberService.update(member);
                        AjaxUtils.renderJson(response,ResultPageDOHelper.getMsgResultDO(null,"success!"));
                    }
                }
            }else {
                AjaxUtils.renderJson(response,ResultPageDOHelper.getErrorResultDO(-2,"邀请码不存在"));
            }
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJson(response,ResultPageDOHelper.getErrorResultDO(-1,"error!"));
        }
    }
}

